<?php 
class listAction extends AdminMaster{
	public function preExecute(){
		$this->formFilter['filter_cate_id'] = '-1';
		$this->data['filter_form_cate']		= NewsCatesTable::getDataList(null);
		parent::preExecute();
	}
	public function execute($request){
		$this->data['navigation']	= __('module_news',null,'menu').' › '. __('module_news_action_news',null,'menu');
		$this->formData				= $request->getParameter('News');	//get form data from add new || edit page

		switch ($this->data['action']){
			case ADMIN_DEFINED::DELETE_RECORD:
				$this->delRecord();
				break;
			case ADMIN_DEFINED::DISABLE:
				$this->disableRecord();
				break;
			case ADMIN_DEFINED::ENABLE:
				$this->enableRecord();
				break;
			case ADMIN_DEFINED::ADD_RECORD:
				$this->addRecord();
				break;
			case ADMIN_DEFINED::EDIT_RECORD:
				$this->editRecord();
				break;
			case ADMIN_DEFINED::SAVE_ORDER:
				$this->saveOrder();
				break;
			#MORE ACTION HERE
			case ADMIN_DEFINED::SET_PROMOTION:
				$this->setPromotion();
				break;
			case ADMIN_DEFINED::SET_HOT:
				$this->setHot();
				break;
			case ADMIN_DEFINED::SET_READMAX:
					$this->setReadmax();
					break;
			#END
			default:
				$this->filterList();
				break;
		}

		$this->setVar('data', $this->data);
	}
	private function filterList(){
		$this->setAction();
		$this->data['navigation'].=' › '. __('nav_list',null,'table_list');

		$this->data['list']			 		= NewsTable::getDataList($this->data['filter'], $this->data['pagination']);
		$this->data['list_total']			= NewsTable::getDataListCount($this->data['filter']);
		$pag	= new PaginationHelper($this->data['list_total'], $this->data['pagination']['limit'], $this->data['pagination']['page_show'], $this->data['pagination']['page'], 'news_list');
		$this->data['pagination_pager']	= $pag->getPaginations();
	}
	private function delRecord(){
		//checking user
		NewsTable::delete($this->data['select_list']);
		$this->filterList();
	}
	private function disableRecord(){
		//checking user
		NewsTable::disable($this->data['select_list']);
		$this->filterList();
	}
	private function enableRecord(){
		//checking user
		NewsTable::enable($this->data['select_list']);
		$this->filterList();
	}
	private function saveOrder(){
		$sortData = array();
		for($i = 0; $i< count($this->data['ids']); $i++){
			$sortData[$this->data['ids'][$i]] = $this->data['sort_orders'][$i];
		}
		News::saveSortOrder($sortData);
		$this->filterList();
	}
	private function setHot(){
		$jsonOut = array('status'=>'0');
		if(isset($this->data['ajax_value'])){
			if(NewsTable::setHot($this->data['id'],$this->data['ajax_value'])){
				$jsonOut['status'] = '1';
				if(intval($this->data['ajax_value'])==1){
					$jsonOut['data'] = array(
							'src'	=> '/css/admin/img/global/control-enable.png',
							'value'	=> '0',
							'title'	=> __('control_set_is_hot_0',null,'news')
					);
				}else{
					$jsonOut['data'] = array(
							'src'	=> '/css/admin/img/global/control-delete.png',
							'value'	=> '1',
							'title'	=> __('control_set_is_hot_1',null,'news')
					);
				}

			}
		}
		print_r(json_encode($jsonOut));die;
		return sfView::NONE;
	}
	private function setPromotion(){
		$jsonOut = array('status'=>'0');
		if(isset($this->data['ajax_value'])){
			if(NewsTable::setPromotion($this->data['id'],$this->data['ajax_value'])){
				$jsonOut['status'] = '1';
				if(intval($this->data['ajax_value'])==1){
					$jsonOut['data'] = array(
							'src'	=> '/css/admin/img/global/control-enable.png',
							'value'	=> '0',
							'title'	=> __('control_set_is_promotion_0',null,'news')
					);
				}else{
					$jsonOut['data'] = array(
							'src'	=> '/css/admin/img/global/control-delete.png',
							'value'	=> '1',
							'title'	=> __('control_set_is_promotion_1',null,'news')
					);
				}

			}
		}
		print_r(json_encode($jsonOut));die;
		return sfView::NONE;
	}
	private function setReadmax(){
		$jsonOut = array('status'=>'0');
		if(isset($this->data['ajax_value'])){
			if(NewsTable::setReadmax($this->data['id'],$this->data['ajax_value'])){
				$jsonOut['status'] = '1';
				if(intval($this->data['ajax_value'])==1){
					$jsonOut['data'] = array(
							'src'	=> '/css/admin/img/global/control-enable.png',
							'value'	=> '0',
							'title'	=> __('control_set_is_readmax_0',null,'news')
					);
				}else{
					$jsonOut['data'] = array(
							'src'	=> '/css/admin/img/global/control-delete.png',
							'value'	=> '1',
							'title'	=> __('control_set_is_readmax_1',null,'news')
					);
				}

			}
		}
		print_r(json_encode($jsonOut));die;
		return sfView::NONE;
	}
	/*********************************************************************************
	 *
	* ADD NEW
	*
	**********************************************************************************/
	private function addRecord(){
		$this->data['navigation'].=' › '. __('nav_add_new',null,'table_list');
		$this->data['button_action'] = __('button_add',null,'table_list');
		$this->setTemplate('newsForm');

		$form = new NewsFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$product_id = NewsTable::insert($this->formData);
				if($product_id > 0){
					$this->data['result']	= __('add_new_success', null, 'news');
					$form = new NewsFForm();
				}else{
					$this->data['result']	= sprintf(__('add_new_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
			}
		}
		$this->data['photo_url'] = sfconfig::get('app_uploadControl_no_image');
		$this->setVar('form', $form);
	}

	/*
	 * EDIT
	*/
	private function editRecord(){
		$this->data['navigation'].=' › '. __('nav_edit',null,'table_list');
		$this->data['action'].='&id='.$this->data['id'];
		$this->data['button_action'] = __('button_edit',null,'table_list');
		$this->setTemplate('newsForm');

		$form = new NewsFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(NewsTable::update($this->formData)){
					$this->data['result']	= __('edit_success', null, 'news');
				}else{
					$this->data['result']	= sprintf(__('edit_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
			}
		}else{
			$data = NewsTable::getDataByIdAdmin($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				if(empty($data['photo_url'])){
					$this->data['photo_url'] = sfconfig::get('app_uploadControl_no_image');
				}else{
					$this->data['photo_url'] = $data['photo_url'];
					$this->data['photo_remove'] = 1;
				}
				$this->formData = array(
						'cate_id'			=> $data['cate_id'],
						'title'				=> $data['title'],
						'description'		=> $data['description'],
						'photo_url'			=> $data['photo_url'],
						'content'			=> $data['content'],
						'related_id'		=> $data['related_id'],
						'is_hot'			=> $data['is_hot'],
						'is_promotion'		=> $data['is_promotion'],
						'is_readmax'		=> $data['is_readmax'],
						'tags'				=> $data['tags'],
						'is_enabled'		=> $data['is_enabled'],
						'_csrf_token'=> $form->getCSRFToken()
				);
				$form->bind($this->formData);

			}
		}
		$this->setVar('form', $form);

	}
	#ACTION FOR EACH ROW
	private function setAction(){
		$action = array();
		$action[]=array(
				'href'	=>	'?action_name='.ADMIN_DEFINED::EDIT_RECORD.'&id=%s',
				'text'	=>	__('action_edit',null,'table_list'),
				'title' =>	__('action_edit',null,'table_list'),
		);

		$this->data['action_list']	= $action;
	}
}